const db = wx.cloud.database()
const _ = db.command
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		statusTrue: "进行中",
		statusFalse: "已结束",
		matchs: [],
		statusList: [],
		indexList: []
	},
	deleteMatch(e) {
		var that = this
		let index = e.currentTarget.dataset.index
		wx.showModal({
			title: '提示',
			content: '确定删除么，删除后不可恢复',
			success(res) {
				if (res.confirm) {
					console.log('用户点击确定')
					db.collection("match").doc(that.data.matchs[index]._id).remove().then(res => {
						let matchs = that.data.matchs
						matchs.splice(index, 1)
						let statusList = []
						let indexList = []
						for (let index in matchs) {
							if (matchs[index].status == true) {
								indexList.push(0)
								statusList.push(1)
								continue
							}
							indexList.push(0)
							statusList.push(0)
						}
						that.setData({
							indexList: indexList,
							statusList: statusList,
							matchs: matchs,
						})
						wx.showToast({
							title: '操作成功',
							icon: "none"
						})
					})
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})

	},
	status(e) {
		// console.log(e.currentTarget.dataset.index)
		let index = e.currentTarget.dataset.index
		let status = "statusList[" + index + "]"
		db.collection("match").doc(this.data.matchs[index]._id).update({
			data: {
				status: !this.data.statusList[index]
			}
		}).then(res => {
			this.setData({
				[status]: !this.data.statusList[index]
			})
			wx.showToast({
				title: '操作成功',
				icon: "none"
			})
		})
	},
	detail(e) {
		console.log(e.currentTarget.dataset)
		wx.navigateTo({
			url: '/pages/matchDetail/matchDetail?matchId=' + e.currentTarget.dataset.matchid,
		})
	},
	showMenu(e) {
		// console.log(e.currentTarget.dataset.index)
		let indexList = []
		for (let i in this.data.indexList) {
			indexList.push(0)
		}
		indexList[e.currentTarget.dataset.index] = 1
		this.setData({
			indexList: indexList
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		wx.cloud.callFunction({
			name: "getopenid"
		}).then(eres => {
			console.log(eres.result.openid)
			let openid = eres.result.openid
			db.collection('match').where({ _openid: openid }).get().then(res => {
				console.log(res)
				let statusList = []
				let indexList = []
				for (let index in res.data) {
					if (res.data[index].status == true) {
						indexList.push(0)
						statusList.push(1)
						continue
					}
					indexList.push(0)
					statusList.push(0)
				}
				this.setData({
					indexList: indexList,
					statusList: statusList,
					matchs: res.data,
					openid: openid
				})
			})
		})

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})